import wx

class mzSequenceView(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, pos=(0,0), size=(200,200))
        wx.EVT_PAINT(self, self.onPaint)
        wx.EVT_SIZE(self, self.onSize)
        self.sequence = None
        
        self.tripelDist = 5
        self.lineDist = 5
        
        self.windowBorderX = 40
        self.windowBorderY = 10                
        
        self.Show(True)

    def drawSequence(self, t_dc):
        if self.sequence != None:
            # get the size of the panel
            size = self.GetSize()
            width = size.GetWidth()
           
            # get the data string
            data = self.sequence.data
            
            lineCount = 0
            textWidth = 0
            textHeight = 0
            
            xPos = self.windowBorderX
            yPos = self.windowBorderY
            
            if not self.sequence.charLengthChecked:
                self.sequence.getMaxLetterWidth(t_dc)
                                                        
            tripletEnd = self.sequence.readStart
            
            while tripletEnd < len(data) + 3:  
                if tripletEnd < 3:
                    substring = data[0:tripletEnd]
                elif tripletEnd > len(data):
                    substring = data[tripletEnd - 3:len(data)]
                else:              
                    substring = data[tripletEnd - 3:tripletEnd]
                    
                textSize = t_dc.GetTextExtent(substring)
                    
                # Check if text fits into current line
                if xPos > width - self.windowBorderX - 3*self.sequence.charMaxWidth:                                                                        
                    yPos = yPos + textSize[1] + self.lineDist
                    xPos = self.windowBorderX
                                                                                                           
                t_dc.DrawText(substring, xPos, yPos)
                    
                xPos = xPos + 3*self.sequence.charMaxWidth + self.tripelDist                
                
                tripletEnd += 3                                     
 
    def onPaint(self, event):
        a_dc = wx.BufferedPaintDC (self)
        a_dc.Clear()
        
        #a_dc.SetPen("Red")
        self.drawSequence(a_dc)
                
    def onSize(self, event):
        self.Refresh()
        self.Update()
        